<!-- 件资封账作业：蹇泽阳 -->
<template>
  <div class="app-container">
    <!-- <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" /> -->
    <div class="mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex">
          <h1 class="f16">员工产值核算封账月份明细</h1>
          <JDialog
            :tableHeader="JTableHeader"
            :width="30"
            @removeOne="removeOne"
            :currentTableRow="currentTableRow"
            @confirm="confirm"
            :unEdit="false"
          />
        </div>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" @currentRow="currentRow" />
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { formateData } from '@/plugins/mixinJ'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PieceCapitalSealedAccountOperation'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

// 表一模块
// 表一按钮

const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>(`/SealAccount/getProductAccountDetails`, {
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.detailsList
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
// watch(() => formList.value.map((item: any) => { item.selectValue, item.inputValue, item.dateValue, item.checked }), () => {
//   getTableList()
// })
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
const tableHeader = ref([
  {
    change: true,
    type: 'selectJ',
    label: '类型',
    prop: 'bonusType',
    options: [
      {
        value: '针车',
        label: '针车'
      },
      {
        value: '裁断',
        label: '裁断'
      }
    ]
  },
  {
    label: '封账月份',
    prop: 'bonusYM'
  },
  {
    change: true,
    label: '输入用户',
    prop: 'sysUser'
  },
  {
    change: true,
    label: '输入时间',
    prop: 'sysDate',
    width: '200'
  }
])
const JTableHeader = ref([
  {
    change: true,
    type: 'selectJ',
    label: '类型',
    prop: 'bonusType',
    options: [
      {
        value: '针车',
        label: '针车'
      },
      {
        value: '裁断',
        label: '裁断'
      }
    ]
  },
  {
    label: '封账月份',
    prop: 'bonusYM'
  }
])
// Dialog确认添加/修改按钮
const confirm = async (obj: any, dialogVisible: any, title: any) => {
  // 打印出obj的值
  const tempList = formateData([obj]) as any
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await LoginWork.post(`/SealAccount/addProductAccountDetails`, {
      ...tempList[0],
      bonusType: obj.bonusType.value
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  } else if (title === '修改') {
    const res = (await LoginWork.post(`/SealAccount/updateProductAccountDetails`, {
      ...tempList[0],
      bonusYMAfter: tempList[0].bonusYM,
      bonusYM: currentTableRow.value.bonusYM
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  }
  // 关闭弹出框
  dialogVisible.value = false
  // 重新获取表格数据
  getTableList()
}
// 删除
const removeOne = async () => {
  // 向服务器发送请求，更新外派单信息
  const res = (await LoginWork.post(`/SealAccount/deleteProductAccountDetails`, currentTableRow.value)) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.message)
  } else {
    JMessage.error(res.message)
  }
  // 重新获取表格数据
  getTableList()
}
</script>

<style lang="less" scoped></style>
